package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.auth.domain.Role;
import cn.ronghuanet.auth.dto.RolePermissionDTO;
import cn.ronghuanet.auth.query.RoleQuery;
import cn.ronghuanet.auth.service.IRoleService;
import cn.ronghuanet.basic.util.AjaxResult;
import cn.ronghuanet.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@RonghuaPermission(name = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    // 新增/修改
    @PutMapping
    @RonghuaPermission(name = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if(Objects.isNull(role.getId())){
                roleService.add(role);
            }else{
                roleService.update(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    // 根据ID删除
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "根据ID删除")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            roleService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    // 批量删除
    @PatchMapping
    @RonghuaPermission(name = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    // 根据ID查询
    @GetMapping("/{id}")
    @RonghuaPermission(name = "根据ID查询")
    public AjaxResult getById(@PathVariable("id")Long id){
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setResultObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("根据ID查询失败!"+e.getMessage());
        }
    }

    // 查询所有
    @GetMapping
    @RonghuaPermission(name = "查询所有")
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return AjaxResult.me().setResultObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询所有失败!"+e.getMessage());
        }
    }

    // 分页查询
    @PostMapping
    public AjaxResult pageList(@RequestBody RoleQuery query){
        try {
            PageList<Role> pageList = roleService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    /**
     * 设置角色的权限
     * 如果前端传递的参数,后端没有与之对应的domain来接收,那么我们可以用Map接收,或者重新定义个DTO对象
     * @param dto
     * @return
     */
    @PostMapping("/permission")
    public AjaxResult setRolePermisison(@RequestBody RolePermissionDTO dto){
        try {
            roleService.setRolePermisison(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    /**
     * 通过角色ID获取该角色绑定的权限SN
     * @param roleId
     * @return
     */
    @GetMapping("/permission/{roleId}")
    public AjaxResult getPermissionSnsByRoleId(@PathVariable("roleId")Long roleId){
        try {
            List<String> permissionSns = roleService.getPermissionSnsByRoleId(roleId);
            return AjaxResult.me().setResultObj(permissionSns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取权限sn失败!"+e.getMessage());
        }
    }
}
